<?php

namespace App\Models;

use Illuminate\Auth\Authenticatable;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Laravel\Lumen\Auth\Authorizable;
use DateTimeInterface;

class Crm_Crowd extends Model implements AuthenticatableContract, AuthorizableContract
{
    use Authenticatable, Authorizable, HasFactory;
    /**
     * 关联到模型的数据表
     *
     * @var string
     */
    protected $table = 'crm_crowd';

    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $primaryKey = 'id';

    protected $fillable = [
        'agencyid','name','cover','type','price','desc','order','backimg','nums','isopen','gender','isreal',
        'province','province_name','city','city_name','area','area_name','reside_province','reside_province_name',
        'reside_city','reside_city_name','reside_area','reside_area_name',
        'maxage','minage','marry','education','house','car'
    ];

    protected $hidden = [
        'is_del',
    ];

    public function scopeNodel($query)
    {
        return $query->where('is_del', 'N');
    }

    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    public static function getFilter($filter)
    {
        return self::where(function ($query) use ($filter){
            if (isset($filter['name']) && !empty($filter['name'])) {
                $query->where('name', 'LIKE','%'.$filter['name'].'%');
            }
            if (isset($filter['isopen']) && !empty($filter['isopen'])) {
                $query->where('isopen', $filter['isopen']);
            }
            $query->where('agencyid', $filter['aid']);
        })->orderBy('id', 'desc');
    }
}
